package ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.presentation.controller.admin;

import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.context.Flash;

import ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.persistence.model.Alarm;
import ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.persistence.model.CompoundCommand;
import ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.persistence.model.DeviceType;
import ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.persistence.model.Field;
import ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.persistence.model.SimpleCommand;
import ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.persistence.model.SoundRequest;
import ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.presentation.controller.admin.devtypes.DeviceTypeBean;
import ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.service.SystemService;

@ManagedBean(name="deviceTypeListBean")
@ViewScoped
public class DeviceTypesListBean {
	

	@ManagedProperty(value="#{systemService}")
	private SystemService sysService;
	
	
	public List<DeviceTypeBean> getDeviceTypes(){
		List<DeviceType> sysDevs;
		sysDevs = sysService.getSysDeviceTypes();
		System.out.println("Device Types. " + sysDevs.size());
		return DeviceTypeBean.wrap(sysDevs,sysService);
	}
	
	public void newDevice(){
		DeviceType newType = new DeviceType();
		newType.setAlarms(new ArrayList<Alarm>());
		newType.setCompoundCommands(new ArrayList<CompoundCommand>());
		newType.setSimpleCommands(new ArrayList<SimpleCommand>());
		newType.setFields(new ArrayList<Field>());
		newType.setName("New "+newType.hashCode());
		newType.setSoundRequests(new ArrayList<SoundRequest>());

		Flash flash = FacesContext.getCurrentInstance().getExternalContext().getFlash();
		flash.put("selectedType", new DeviceTypeBean(newType, sysService));
	}
	
	public SystemService getSysService() {
		return sysService;
	}
	
	public void setSysService(SystemService sysService) {
		this.sysService = sysService;
	}
}
